"use strict";

/**
 * @ngdoc function
 * @name jgappApp.controller:FinanceFZzsdkCtrl
 * @description
 * # FinanceFZzsdkCtrl
 * Controller of the jgappApp
 */
angular
  .module("jgappApp")
  .controller("FinanceFZzsdkCtrl", function(
    $scope,
    openApi,
    Util,
    $location,
    $routeParams,
    FormSubmit
  ) {
    var id = ($scope.process_id = $routeParams.id);
    $scope.chooseinfo = function() {
      var relModuleObj = {
        name: "FinanceFZzsdkCtrl",
        selectTab: "projectName",
        selectTabContent: "工程合同"
      };
      // 清掉关联的编号
      var params = $scope.param;
      Util.savaParamForReview(params, id);
      Util.savaDataforsearch(relModuleObj);
      $location.path("finance/choosecontact");
    };
    $scope.chooseCommonContact = function() {
      if (!$scope.param.projectName) {
        Util.Toast("请先选择一个工程合同");
        return;
      }
      var relModuleObj = {
        name: "FinanceFZzsdkCtrl",
        selectTabContent: "通用合同",
        param: {
          contractNo: $scope.param.projectContractNo
        }
      };
      // 清掉关联的编号
      var params = $scope.param;
      Util.savaParamForReview(params, id);
      Util.savaDataforsearch(relModuleObj);
      $location.path("finance/choosetycontact");
    };
    $scope.chooseInvoice = function() {
      if (!$scope.param.projectName) {
        Util.Toast("请先选择一个工程合同");
        return;
      }
      var relModuleObj = {
        name: "FinanceFZzsdkCtrl",
        selectTabContent: "成本发票",
        param: {
          contractNo: $scope.param.projectContractNo,
          commonContractNo: $scope.param.commonContractNo
        }
      };
      // 清掉关联的编号
      var params = $scope.param;
      Util.savaParamForReview(params, id);
      Util.savaDataforsearch(relModuleObj);
      Util.saveSelectLists($scope.param.details);
      $location.path("finance/choosecbinvoice");
    };
    $scope.swipe = function(index) {
      $scope.index = index;
    };
    $scope.resetlist = function(index) {
      $scope.index = -1;
    };
    // 删除证件信息列表
    $scope.deletedItem = function(index) {
      $scope.param.details.splice(index, 1);
      $scope.index = -1;
      Util.saveSelectLists($scope.param.details);
    };

    $scope.submit = function(operationId) {
      // 底部类型end
      var options = {};
      // 流程id
      options.id = id;
      // 根据operationId判断是草稿还是提交
      options.operationId = operationId;
      options.urlParam = openApi.taxDeduction;
      $scope.param.invoiceIds = $scope.param.details.map(function(item) {
        return item.invoiceId||item.pkId;
      });
      options.param = angular.copy($scope.param, {});
      delete options.param.details;
      FormSubmit.submit(options, $scope);
    };
    $scope.validate = function() {
      if (!$scope.param.projectName) {
        Util.Toast("请选择工程名称");
        return;
      }
      if (!$scope.param.deductionAmount) {
          if ($scope.param.deductionAmount !== 0) {
              Util.Toast("请填写抵扣金额");
              return;
          }
        }
      if (!$scope.param.details || $scope.param.details.length < 1) {
        Util.Toast("请选择成本发票");
        return;
      }
      // if (!$scope.validation.passvalidation) {
      //   Util.Toast($scope.validation.tip);
      //   return;
      // }
      return true;
    };
    var init = function() {
      // 获取回显的详细信息
      Util.getDetailData(id, function(data) {
        $scope.param = data;
        // 获取选择页面返回的数据并且合并到参数中
        var selectLists = Util.getSelectLists();
        // 获取选择页面返回的数据并且合并到参数中
        $scope.param.details =
          selectLists || $scope.param.details || [];
      });
    };
    // 初始化
    init();
  });
